<?

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/custom/groupusers.php
#	FUNCTION:		Custom handler for the Group Users page
#	AUTHOR:			Cameron Morrow
#	CREATED:		03/08/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Get group names and IDs
$group_names = getAllGroupNames(false, true);
$group_ids = getAllGroupIDs(false, true);

# Check group ID passed
$group_id = (@$_GET["group"]) ? (integer) $_GET["group"] : 1;

$group_name = $group_names[findInArray($group_ids, $group_id)];

# See if user ID was passed to move to this group
if (@$_POST["userid"]) {
	
	# Get ID
	$group_id_new="";
	
	$user_id = $_POST["userid"];
	
	$selectSql="SELECT * FROM ".$PROJECT_DB_TABLES["users"]." WHERE u_id = \"" . $user_id . "\"";
	
	$userdata = getRecord($selectSql);
	
	$userGroups=explode(",",$userdata[0]["u_group"]);
	
	$usergroup_admin_flag = false;
	
	$usergroup_developer_flag = false;
	
	foreach($userGroups as $group_key=>$group_value){
		if($group_value=="2"){
			$usergroup_admin_flag=true;
		}
		else if($group_value=="3"){
			$usergroup_developer_flag=true;
		}
	}
	
	
	if($usergroup_admin_flag==true && $usergroup_developer_flag==false){
		$group_id_new=$group_id;
	}
	else if($usergroup_developer_flag==true){
		$group_id_new=$group_id;
	}
	else if($usergroup_admin_flag==false && $usergroup_developer_flag==false){;
		if($group_id=="2" || $group_id=="3"){
			$group_id_new=$group_id;
		}
		else{
			$group_id_new=implode(",",$userGroups).",".$group_id;
		}
	}
	
	# SQL
	$sql = "UPDATE " . $PROJECT_DB_TABLES["users"] . " SET u_group = \"" . $group_id_new . "\" WHERE u_id = \"" . $user_id . "\"";

	# Execute
	if (executeSQLQuery($sql)) {
		addMessage("<p>OK, user " . $user_id . " was assigned to the " . $group_name . " group.</p>");

		addLog($user_id . "|" . $group_id, 15);
	} else {
		addMessage("<p>Sorry, an error occured and the user " . $user_id . " could not be assigned to the " . $group_name . " group.</p>", 2);
	}
}

# Find user to modify
addContent("<h2>Find User for " . $group_name . "</h2>" .
	"<p>Search for a user to move to this group:</p>\n" .
	"<form name=\"userfindform\" id=\"userfindform\" action=\"" . $PAGE_ID . ".php?group=" . $group_id . "\" method=\"post\">" .
	"<p>User: <input type=\"text\" id=\"search\" name=\"search\" value=\"" . $SEARCH_TERM . "\" /> <input type=\"submit\" value=\"Go\" /></p>" .
	"</form>");

if ($SEARCH_TERM != "") {

	# If search was submitted execute now
	$sql = "SELECT u_id, u_fname, u_lname, u_group, g_name FROM " . $PROJECT_DB_TABLES["users"] . ", " . $PROJECT_DB_TABLES["groups"] . " WHERE u_group = g_id AND u_group !=" . $group_id . " AND (";

	# Fields
	foreach (array("u_id", "u_fname", "u_lname") as $field_count => $field) {

		# Terms
		foreach (explode(" ", $SEARCH_TERM) as $term_count => $term) {
			$sql .= $field . " LIKE \"%" . $term . "%\"";
			if ($term_count < count(explode(" ", $SEARCH_TERM)) - 1) {
				$sql .= " OR ";
			}
		}

		if ($field_count < 2) {
			$sql .= " OR ";
		}
	}
	
	$sql .= ") ";

	# Limit to users in available group
	$sql .= " AND (";
		foreach ($group_ids as $counter => $group_id_item) {
			$sql .= "u_group = " . $group_id_item;
			if ($counter < count($group_ids) - 1) {
				$sql .= " OR ";
			}
		}
	$sql .= ")";
	
	# Order
	$sql .= " ORDER BY u_group, u_id ASC";
	

	# Get results
	$current_data = getRecord($sql);

	# Prep a table properties object for the users
	$data_properties = new TableProperties();
	
	$actionFormat="";
	if($group_name=="Administrators" || $group_name=="Developers"){
		$actionFormat="\"<input type=\\\"button\\\" onclick=\\\"moveUserToGroup('[u_id]');\\\" value=\\\"Move to " . $group_name . "\\\" />\"";
	}
	else{
		$actionFormat="\"<input type=\\\"button\\\" onclick=\\\"moveUserToGroup('[u_id]');\\\" value=\\\"Add " . $group_name . "\\\" />\"";
	}
	$data_properties -> addCustomColumn("User", "\"[u_id]<br /><em class=\\\"supplement\\\">[u_fname] [u_lname]</em>\"");
	$data_properties -> addCustomColumn("Action",$actionFormat);
	

	# If results found
	if (count($current_data) > 0) {

		# Draw the table	
		addContent($data_properties -> drawBrowseTable($current_data, "Results", "g_name"));
	} else {

		addContent("<h2>No results found</h2>" .
			"<p>Sorry, there were no results found for your search. It is possible that there were users found who matched the search parameters, but who were already assigned to the " . $group_name . " group.</p>");
	}
	
	# Add form
	addContent("<form name=\"moveuserform\" id=\"moveuserform\" action=\"" . $PAGE_ID . ".php?group=" . $group_id . "\" method=\"post\">" .
		"<input type=\"hidden\" id=\"userid\" name=\"userid\" value=\"\" />" .
		"<input type=\"hidden\" id=\"searchmove\" name=\"search\" value=\"" . $SEARCH_TERM . "\" />" .
		"</form>");	
}

# Close Button
addContent("<div class=\"closebutton\"><input type=\"button\" value=\"Close\" onclick=\"window.close();\" /></div>");

?>